﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mediatheque.Areas.Admin.Models;

namespace mediatheque.Areas.Admin.Controllers
{
    public class FilmsController : Controller
    {
        protected Oeuvres myOeuvre = new Oeuvres();
        //
        // GET: /Admin/Films/

        /****************************************************
         * 
         *  Gestion des films
         * 
         * 
         **/

        public ActionResult Index()
        {
            return View("listeFilm", myOeuvre);
        }

        public ActionResult Add()
        {
            return View("addFilm");
        }

        [HttpPost]
        public ActionResult Add(FormCollection data)
        {
            try
            {
                movies m = new movies
                {
                    title = data["title"],
                    length = int.Parse(data["length"]),
                    release_year = int.Parse(data["realase_year"]),
                    resume = data["resume"]
                };

                if (TryUpdateModel(m))
                {
                    myOeuvre.movies.AddObject(m);
                    myOeuvre.SaveChanges();
                }
                return RedirectToAction("Edit", new { id = m.id });
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Info(int id)
        {
            var m = myOeuvre.getMoviesById(id);
            if (m == null)
                return View("NotFound");
            else
                return View("ficheFilm", m);
        }

        public ActionResult Edit(int id)
        {
            var m = myOeuvre.getMoviesById(id);
            if (m == null)
                return View("NotFound");
            else
                return View("editFilm", myOeuvre);
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var o = myOeuvre.movies.First(i => i.id == id);
            try
            {
                if (o == null)
                    return View("NotFound");
                else
                {
                    o.title = collection["title"];
                    o.length = int.Parse(collection["length"]);
                    o.release_year = int.Parse(collection["release_year"]);
                    o.resume = collection["resume"];
                    myOeuvre.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Edit", new { id = o.id });
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                var o = myOeuvre.movies.First(i => i.id == id);
                myOeuvre.movies.DeleteObject(o);
                myOeuvre.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult Categorized(int id, FormCollection data)
        {
            if (data.Count > 0)
            {
                categorized cat = new categorized
                {
                    movie = id,
                    category = int.Parse(data["categorie"])
                };

                if (TryUpdateModel(cat))
                {
                    myOeuvre.categorized.AddObject(cat);
                    myOeuvre.SaveChanges();
                }
            }
            return RedirectToAction("Edit", new { id = id });
        }

        public ActionResult Uncategorized(int id, FormCollection data)
        {
            if (data.Count > 0)
            {
                int category = int.Parse(data["categorie"]);
                var o = myOeuvre.categorized.Where(c => c.movie == id)
                                      .Where(c => c.category == category)
                                      .First();
                myOeuvre.categorized.DeleteObject(o);
                myOeuvre.SaveChanges();
            }
            return RedirectToAction("Edit", new { id = id });
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UploadCover(int id, HttpPostedFileBase uploadFile)
        {

            if (uploadFile.ContentLength > 0)
            {
                var fileName = id.ToString() + "" + Path.GetExtension(uploadFile.FileName);
                var path = Path.Combine(Server.MapPath("~/Content/i/media/films"), fileName);
                uploadFile.SaveAs(path);

                var o = myOeuvre.movies.First(i => i.id == id);
                o.cover = fileName;
                myOeuvre.SaveChanges();
            }
            return RedirectToAction("Edit", new { id = id });
        }

        public ActionResult Realized(int id, FormCollection data)
        {
            if (data.Count > 0)
            {
                participate cat = new participate
                {
                    media = id,
                    artiste = int.Parse(data["artiste"]),
                    lib = "real",
                    type = "dvd"
                };

                if (TryUpdateModel(cat))
                {
                    myOeuvre.participate.AddObject(cat);
                    myOeuvre.SaveChanges();
                }
            }
            return RedirectToAction("Edit", new { id = id });
        }

        public ActionResult Unrealized(int id, FormCollection data)
        {
            if (data.Count > 0)
            {
                int artiste = int.Parse(data["artiste"]);
                var o = myOeuvre.participate.Where(c => c.media == id)
                                      .Where(c => c.artiste == artiste)
                                      .Where(c => c.type == "dvd")
                                      .First();
                myOeuvre.participate.DeleteObject(o);
                myOeuvre.SaveChanges();
            }
            return RedirectToAction("Edit", new { id = id });
        }

        public ActionResult Act(int id, FormCollection data)
        {
            if (data.Count > 0)
            {
                participate cat = new participate
                {
                    media = id,
                    artiste = int.Parse(data["artiste"]),
                    lib = "actor",
                    type = "dvd"
                };

                if (TryUpdateModel(cat))
                {
                    myOeuvre.participate.AddObject(cat);
                    myOeuvre.SaveChanges();
                }
            }
            return RedirectToAction("Edit", new { id = id });
        }

        public ActionResult Unact(int id, FormCollection data)
        {
            if (data.Count > 0)
            {
                int artiste = int.Parse(data["artiste"]);
                var o = myOeuvre.participate.Where(c => c.media == id)
                                      .Where(c => c.artiste == artiste)
                                      .Where(c => c.type == "dvd")
                                      .First();
                myOeuvre.participate.DeleteObject(o);
                myOeuvre.SaveChanges();
            }
            return RedirectToAction("Edit", new { id = id });
        }
        /*****************************************************/

    }
}
